package com.turnengine.client.local.location.bean;

import org.apache.commons.lang.builder.CompareToBuilder;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

/**
 * The Coordinate Grid.
 */
public class CoordinateGrid implements ICoordinateGrid, Comparable<ICoordinateGrid> {

	/** The length. */
	private int length = 0;
	/** The width. */
	private int width = 0;

	/**
	 * Creates a new Coordinate Grid.
	 */
	public CoordinateGrid() {
	}

	/**
	 * Creates a new Coordinate Grid.
	 * @param length the length
	 * @param width the width
	 */
	public CoordinateGrid(int length, int width) {
		setLength(length);
		setWidth(width);
	}

	/**
	 * Creates a new Coordinate Grid.
	 * @param coordinateGrid the coordinate grid
	 */
	public CoordinateGrid(CoordinateGrid coordinateGrid) {
		setLength(coordinateGrid.getLength());
		setWidth(coordinateGrid.getWidth());
	}

	/**
	 * Creates a new Coordinate Grid.
	 * @param iCoordinateGrid the i coordinate grid
	 */
	public CoordinateGrid(ICoordinateGrid iCoordinateGrid) {
		setLength(iCoordinateGrid.getLength());
		setWidth(iCoordinateGrid.getWidth());
	}

	/**
	 * Returns the length.
	 * @return the length.
	 */
	public int getLength() {
		return length;
	}

	/**
	 * Returns the width.
	 * @return the width.
	 */
	public int getWidth() {
		return width;
	}

	/**
	 * Sets the length.
	 * @param length the length to set.
	 */
	public void setLength(int length) {
		this.length = length;
	}

	/**
	 * Sets the width.
	 * @param width the width to set.
	 */
	public void setWidth(int width) {
		this.width = width;
	}

	/**
	 * Returns the hash code.
	 * @return the hash code.
	 */
	@Override
	public int hashCode() {
		HashCodeBuilder hash = new HashCodeBuilder(691, 37);
		hash.append(length);
		hash.append(width);
		return hash.toHashCode();
	}

	/**
	 * Returns true if this is equal to the given object.
	 * @param object the object to compare.
	 * @return true if this is equal to the given object.
	 */
	@Override
	public boolean equals(Object object) {
		if (object == this) {
			return true;
		}
		if (object instanceof CoordinateGrid) {
			CoordinateGrid compare = (CoordinateGrid) object;
			EqualsBuilder equals = new EqualsBuilder();
			equals.append(this.length, compare.length);
			equals.append(this.width, compare.width);
			return equals.isEquals();
		}
		return false;
	}

	/**
	 * Compare this to the given object.
	 * @param compare the object to compare to.
	 * @return the result of the comparison.
	 */
	@Override
	public int compareTo(ICoordinateGrid compare) {
		CompareToBuilder builder = new CompareToBuilder();
		builder.append(this.length, compare.getLength());
		builder.append(this.width, compare.getWidth());
		return builder.toComparison();
	}

	/**
	 * Returns this as a string.
	 * @return this as a string.
	 */
	@Override
	public String toString() {
		ToStringBuilder string = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE);
		string.append("length", length);
		string.append("width", width);
		return string.toString();
	}
}
